package com.ayi.servlet.frontdesk; /**
 * @author Ayingzz
 * @create 2021/11/29 8:59
 */

import com.ayi.bean.Book;
import com.ayi.bean.User;
import com.ayi.dao.bookdao.QueryBook;
import com.ayi.dao.bookdao.UpdateBook;
import com.ayi.dao.cartdao.AddCart;
import com.ayi.dao.cartdao.QueryCart;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "addBookIntoCartServlet", value = "/addBookIntoCartServlet")
public class addBookIntoCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = new QueryBook().queryBookById(Integer.parseInt(request.getParameter("id")));
        User user = (User) request.getSession().getAttribute("user");

        if (new AddCart().addBookIntoCartByUsername(user.getUsername(), book)) {
            new UpdateBook().updateInventory(book.getId(), -1);
            response.sendRedirect("IndexServlet");
        } else {

        }
    }
}
